package org.hello.spring.web.form.validator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hello.spring.web.form.HelloForm;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class HelloFormValidator implements Validator {
	 /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    public boolean supports(Class clazz) {
        return HelloForm.class.equals(clazz);
    }
    
    public void validate(Object obj, Errors errors) {
    	HelloForm helloForm = (HelloForm) obj;
        if (helloForm == null) {
            errors.rejectValue("message", "error.not-specified");
        }
        else {
            logger.info("Validating with " + helloForm + ": \"" + helloForm.getMessage()+"\"");
            if (helloForm.getMessage().trim().equals("")) {
                errors.rejectValue("message", "error.empty_message",
                    new Object[] {helloForm.getMessage()}, "Message is not valid");
            }
        }
    }
}
